package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.entity.DeliveryWarehouse;
import com.dingreading.cloud.admin.service.DeliveryWarehouseService;
import com.dingreading.cloud.admin.service.WhStockOutDeliveryService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.DescribeException;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/system/deliveryWarehouse")
@Api(tags = "总部-发货仓库管理")
public class DeliveryWarehouseController {

    @Resource
    protected DeliveryWarehouseService deliveryWarehouseService;
    @Resource
    protected WhStockOutDeliveryService stockOutDeliveryService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "仓库名/联系人姓名") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "启用状态") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        Page<DeliveryWarehouse> page = deliveryWarehouseService.pageList(pageUtil, keyword, enabled);

        PageBeanDto data = new PageBeanDto();
        data.setTotal(page.getTotalRow());
        data.setList(page.getRecords());
        return R.ok(data);
    }


    @ApiOperation(value = "获取信息")
    @PostMapping(value = "/getBean", produces = "application/json; charset=UTF-8")
    public R<Object> getBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "仓库id") @RequestParam(value = "id") Long id
    ) {
        DeliveryWarehouse data = this.deliveryWarehouseService.getById(id);
        return R.ok(data);
    }


    @ApiOperation(value = "删除")
    @PostMapping(value = "/dropBean", produces = "application/json; charset=UTF-8")
    public R<Object> dropBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "仓库id") @RequestParam(value = "id") Long id
    ) {
        boolean b = stockOutDeliveryService.haveDeliveryWareHouse(id);
        if (b) return R.fail("仓库发货已使用，不能删除");

        deliveryWarehouseService.removeById(id);
        return R.ok();
    }

    @Transactional(rollbackFor = DescribeException.class)
    @ApiOperation(value = "添加/修改仓库")
    @PostMapping(value = "/newBean", produces = "application/json; charset=UTF-8")
    public R<Object> newBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "仓库id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "仓库类型 1.自营仓库 2.第三方仓库") @RequestParam(value = "type", required = false) Integer type,
            @ApiParam(value = "仓库名称") @RequestParam(value = "name") String name,
            @ApiParam(value = "联系人姓名") @RequestParam(value = "userName", required = false) String userName,
            @ApiParam(value = "联系人号码") @RequestParam(value = "phone", required = false) String phone,
            @ApiParam(value = "详细地址") @RequestParam(value = "address", required = false) String address
    ) {
        if (StringUtils.isBlank(name))
            return R.fail("请填写仓库名称");

        if (null == type || type <= 0)
            return R.fail("请选择仓库类型");

        DeliveryWarehouse data = null;
        if (null == id || id <= 0) {
            data = new DeliveryWarehouse();
            data.setAddStaffUid(headStaffUid);
            data.setAddStaffName(headStaffName);
            data.setType(type);
            data.setEnabled(1);
        } else {
            data = deliveryWarehouseService.getById(id);
            if (null == data) return R.fail("获取失败");
        }
        data.setName(name);
        data.setUserName(userName);
        data.setPhone(phone);
        data.setAddress(address);
        data.setLastStaffUid(headStaffUid);
        data.setLastStaffName(headStaffName);
        deliveryWarehouseService.saveOrUpdate(data);
        return R.ok();
    }


    @ApiOperation(value = "更新仓库状态")
    @PostMapping(value = "/updateEnabled", produces = "application/json; charset=UTF-8")
    public R<Object> updateEnabled(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "仓库id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "启用状态 1.启用 2.禁用") @RequestParam(value = "enabled") Integer enabled
    ) {
        boolean l = deliveryWarehouseService.updateStatus(id, enabled);
        if (!l) return R.fail("更新失败");

        return R.ok();
    }


    @ApiOperation(value = "获取启用的第三方仓库列表")
    @PostMapping(value = "/getThirdPartyWarehouse", produces = "application/json; charset=UTF-8")
    public R<Object> getThirdPartyWarehouse(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName
    ) {
        List<DeliveryWarehouse> list = deliveryWarehouseService.getByType(2);
        return R.ok(list);
    }

}
